package com.nova.bluetooth.ui.ai.translate;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;

import com.kunminx.architecture.domain.message.MutableResult;
import com.nova.bluetooth.MyApp;
import com.nova.bluetooth.base.BaseNovaViewModel;
import com.nova.bluetooth.base.CommonResult;
import com.nova.bluetooth.base.TranslatingBean;
import com.nova.bluetooth.helpUtils.TimeUtil;
import com.nova.bluetooth.netWork.RetrofitRequest;
import com.nova.bluetooth.xunFeiSDK.recorder.SystemRecorder;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class MyTranslateViewModel extends BaseNovaViewModel {
    public MutableLiveData<Boolean> bottomIsAsrSpeaking = new MutableLiveData<>(true);
    public MutableLiveData<Boolean> isTranslating = new MutableLiveData<>(false);
    public MutableLiveData<Boolean> ttsIsOpen = new MutableLiveData<>(false);
    public MutableLiveData<String> asrTargetLanguage = new MutableLiveData<>("zh_cn");
    public MutableLiveData<String> translationTargetLanguage = new MutableLiveData<>("en");
    public final MutableResult<String> emptyText = new MutableResult<>();
    public final MutableLiveData<String> searchStr = new MutableLiveData<>();
    public final LiveData<String> searchStrLD = Transformations.distinctUntilChanged(searchStr);

    /**
     * 长按耳机说话提示View是否可见
     */
    public MutableLiveData<Boolean> isTipsVisible = new MutableLiveData<>(true);
    public MutableLiveData<Boolean> isLanguageTipsVisible = new MutableLiveData<>(true);
    public MutableLiveData<Boolean> isSwitchLanguageVisible = new MutableLiveData<>(true);
    /**
     * 底部水波纹View是否可见
     */
    public MutableLiveData<Boolean> isBottomWaveViewVisible = new MutableLiveData<>(false);
    public MutableLiveData<CommonResult> insertTranslationContentResult = new MutableLiveData<>();
    public MutableLiveData<List<TranslatingBean>> translatingBeanList = new MutableLiveData<>();
    public MutableResult<String> toastMsg = new MutableResult<>();
    public MutableResult<Integer> toastMsgRes = new MutableResult<>();
    private boolean isSearch = false;
    public MutableLiveData<Boolean> isTopLangTipsVisible = new MutableLiveData<>(false);
    public MutableLiveData<Boolean> isBottomLangTipsVisible = new MutableLiveData<>(false);

    public final MutableResult<Boolean> insertSuccess = new MutableResult<>();

    public TranslatingBean getLastTranslatingBean() {
        List<TranslatingBean> list = translatingBeanList.getValue();
        return list.get(list.size() - 1);
    }

//    private TranslateBeanDao translateBeanDao;

    public MyTranslateViewModel() {
//        MyDataBase dataBase = MyDataBase.getMyDataBase();
//        translateBeanDao = dataBase.mTranslateBeanDao();

        List<TranslatingBean> emptyData = new ArrayList<>();
        emptyData.add(new TranslatingBean());  //安全赋值，防止空值出现

//        testData.add(new TranslatingBean("你好","hello","2023-11-04 16:20:00"));
//        testData.add(new TranslatingBean("你好,你好","hello hello","2023-11-04 16:21:00"));
//        testData.add(new TranslatingBean("你好","hello","2023-11-04 16:22:00"));
//        testData.add(new TranslatingBean("你好","hello","2023-11-04 16:23:00"));
//        testData.add(new TranslatingBean("你好","hello","2023-11-04 16:24:00"));
//        testData.add(new TranslatingBean("你好","hello","2023-11-04 16:25:00"));
//        testData.add(new TranslatingBean("你好","hello","2023-11-04 16:26:00"));
//        testData.add(new TranslatingBean("你好","hello","2023-11-04 16:27:00"));
        translatingBeanList.setValue(emptyData);
    }

    public void setSearch(boolean search) {
        isSearch = search;
        if (!isSearch) {
            enableLoadMore.set(true);
            enableRefresh.set(true);
        } else {
            enableLoadMore.set(false);
            enableRefresh.set(false);
        }
    }

    @Override
    public void getPageData(int pageCurrent) {
        super.getPageData(pageCurrent);
//        if (!isSearch) {
//            getTranslationContentList(pageCurrent, pageSize);
//        } else {
//            getTranslationContentListByQueryName(searchStr.getValue());
//        }
    }

//    public void insertTranslate(TranslateBean translateBean) {
//        new Thread(() -> translateBeanDao.insertTranslateBean(translateBean)).start();
//
//    }
//
//    private final MutableLiveData<List<TranslateBean>> allTranslateBeanData =
//            new MutableLiveData<>();
//
//    public MutableLiveData<List<TranslateBean>> getAllTranslateBeanData() {
//        return allTranslateBeanData;
//    }
//
//    public void getAllTranslateBeanList() {
//        new Thread(() -> allTranslateBeanData.postValue(translateBeanDao.getAllTranslateBeanList())).start();
//    }


    private final MutableLiveData<TextTranslateResult> textTranslateResultData =
            new MutableLiveData<>();

    public MutableLiveData<TextTranslateResult> getTextTranslateResultData() {
        return textTranslateResultData;
    }


//    public void textTranslate(Map map) {
//        RetrofitRequest.textTranslate(map, TextTranslateResult.class,
//                new RetrofitRequest.ResultHandler<TextTranslateResult>(MyApplication.getInstance()) {
//                    @Override
//                    public void onBeforeResult() {
//
//                    }
//
//                    @Override
//                    public void onResult(TextTranslateResult textTranslateResult) {
//                        if (textTranslateResult.getCode() == 200) {
//                            textTranslateResultData.postValue(textTranslateResult);
//                        }
//                    }
//
//                    @Override
//                    public void onAfterFailure() {
//
//                    }
//                });
//    }

    public MutableLiveData<TextTranslateResult> baiduTranslationResult = new MutableLiveData<>();

    public void baiduTranslation(String text, String language) {
        if (language.equals("zh_henan")){  //因为百度翻译没有河南话翻译，所以将河南识别code 纠正为中文（zh）
            language = "zh";
        }
        BaiduTranslateRequest request = new BaiduTranslateRequest();
        request.setText(text);
        request.setCode(language);
        RetrofitRequest.baiduTranslation(request, TextTranslateResult.class,
                new RetrofitRequest.ResultHandler<TextTranslateResult>(MyApp.getInstance()) {
                    @Override
                    public void onBeforeResult() {
                        isTranslating.setValue(true);
                    }

                    @Override
                    public void onResult(TextTranslateResult textTranslateResult) {
                        isTranslating.setValue(false);

                        if (Objects.equals(textTranslateResult.getCode(), 200)) {
                            List<TranslatingBean> list = translatingBeanList.getValue();
                            assert list != null;
                            TranslatingBean lastBean = list.get(list.size() - 1);

                            long timeTag = SystemRecorder.saveRecordAudioTimeSnippet;
                            lastBean.setCreateTime(TimeUtil.formatTimestamp(timeTag));
                            lastBean.setTranslatedContent(textTranslateResult.getData());
                            lastBean.setOriginalContent(text);

                            list.set(list.size() - 1, lastBean);
                            setContentToTargetList(text, textTranslateResult.getData());

                            baiduTranslationResult.setValue(textTranslateResult);

                            list.add(new TranslatingBean()); //加个空的item，给下次语言识别的内容留个空位
                        }
                    }

                    @Override
                    public void onAfterFailure() {
                        isTranslating.setValue(false);
                    }
                });
    }



    /**
     * 根据底部语言类型和翻译目标语言类型确定列表显示内容的位置
     * @param asrSpeakingText  语音识别的文字显示
     * @param translateText  翻译的文字显示
     */
    public void setContentToTargetList(String asrSpeakingText,
                                       String translateText) {
        List<TranslatingBean> list = translatingBeanList.getValue();
        assert list != null;
        TranslatingBean lastBean = list.get(list.size() - 1);

        if (Boolean.TRUE.equals(bottomIsAsrSpeaking.getValue())) {//当前是否点击的底部语音识别中
            lastBean.setBottomContent(asrSpeakingText);
            lastBean.setTopContent(translateText);
        } else {
            lastBean.setBottomContent(translateText);
            lastBean.setTopContent(asrSpeakingText);
        }
        list.set(list.size() - 1, lastBean);
        translatingBeanList.setValue(list);
    }
}
